
rm(list = ls())
setwd("./Fig.2B")


library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(dplyr) # A Grammar of Data Manipulation
library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package
library(plyr) # Tools for Splitting, Applying and Combining Data
library(openxlsx)
require(magrittr)

df<-read.xlsx("flavonoid metabolome.xlsx", sheet=1, check.names = F, sep = "\t")
df
names(df)



df$x <- 1:5
##绘图


names(df)
##1.All
df3 <- melt(df, id.vars = c("sample","group"), 
            measure.vars = c('Flavones..Flavonols','Flavonoids','Flavanones',
                             'Anthocyanins','Isoflavonoids','Chalcones..dihydrochalcones'))


df3$group <- factor(df3$group,levels = c("groupA","groupB","groupC","groupD"))

df4 <- df3 %>%
  select(sample,value) %>%
  group_by(sample) %>% 
  summarise_all(sum)
df4 <- df4[order(abs(df4$value),decreasing=F),]

df4$x <- 1:5

df3$sample <- factor(df3$sample,levels = df4$sample)
df3

df3 <- left_join(df3, df4[c(1,3)], by = "sample")

##绘图
df3 %>% 
  dplyr::filter(sample%>%
                  is.na%>%
                  not)%>%
  dplyr::mutate(x=x%>%
                  factor(levels=c("3","4","2","5","1")))%>%
  ggplot(aes(x, value))+

  geom_col(aes(fill = variable))+
  coord_polar(theta = 'y')+
 
  geom_text(data = df4, aes(x = x, y = 0, label = sample),
            hjust = 1, size = 3.5, show.legend = F)+
  theme_void()+
  theme(legend.position = c(0.2,0.78),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 14, color = "red"))+
  guides(fill=guide_legend(ncol = 1, keywidth=1.3, keyheight=1))+
  #自定义分组颜色
  scale_fill_manual(values = c("#ec1c24","#fdbd10","#0066b2","#ed7902","#acc236","grey"))



df5 <- ddply(df3, 'sample', transform, percent_con = value/sum(value)*100)

df6 <- df5 %>% 
  dplyr::filter(sample%>%
                  is.na%>%
                  not)%>%
  dplyr::mutate(x=x%>%
                  factor(levels=c(3,4,2,5,1),
                         labels=rev(c(1,2,3,4,5)))%>%
                  as.character%>%
                  as.numeric)%>%
  ggplot(aes(x, percent_con))+
  geom_col(aes(fill = variable))+

  coord_polar(theta = 'y')+

  scale_x_continuous(limits = c(-2,9.5))+

  scale_y_continuous(limits = c(0,130))+

  geom_text(data = df4%>%
              dplyr::filter(sample%>%
                              is.na%>%
                              not)%>%
              dplyr::mutate(x=x%>%
                              factor(levels=c(3,4,2,5,1),
                                     labels=rev(c(1,2,3,4,5)))%>%
                              as.character%>%
                              as.numeric), aes(x = x, y = 0, label = sample),
            hjust = 1, size = 3.5, show.legend = F)+

  theme_void()+
  theme(legend.position = c(0.2,0.78),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 14, color = "red"))+
  guides(fill=guide_legend(ncol = 1, keywidth=1.3, keyheight=1))+

  scale_fill_manual(values = c("#d20962","#f47721","#7ac143","#00a78e","#00bce4","red"))
df6
ggsave("1ALL.svg",df6)

#------#
#sheet2#
#------#
require(tidyr)
df<-read.xlsx("1_data.xlsx", sheet=2,check.names = F, sep = "\t")
df%>%
  dplyr::select(-group)%>%
  gather(variable,value,-sample)%>%
  dplyr::mutate(sample=sample%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(sample)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(sample)))%>%
  dplyr::mutate(x=sample%>%
                  factor(levels=levels(sample),
                         labels=5:1)%>%
                  as.character%>%
                  as.numeric)%>%
  dplyr::left_join(df%>%
                     dplyr::select(-group)%>%
                     gather(variable,value,-sample)%>%
                     dplyr::group_by(sample)%>%
                     dplyr::summarise(sum=sum(value)),
                   by="sample")%>%
  dplyr::mutate(percent_con=value*100/sum)%>%
  dplyr::mutate(variable=variable%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(variable)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(variable),
                         labels=paste("Value",1:ncol(dplyr::select(df,starts_with("Value"))))))->ggData

Fig2 <- ggData%>%
  ggplot(aes(x, percent_con))+
 
  geom_col(aes(fill = variable))+

  coord_polar(theta = 'y')+
  scale_x_continuous(limits = c(-2,9.5))+
  
  scale_y_continuous(limits = c(0,130))+

  geom_text(data = ggData%>%
              dplyr::select(sample,x)%>%
              unique, aes(x = x, y = 0, label = sample),
            hjust = 1, size = 3.5, show.legend = F)+

  theme_void()+
  theme(legend.position = c(0.2,0.78),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 14, color = "red"))+
  guides(fill=guide_legend(ncol = 1, keywidth=1.3, keyheight=1))+
  scale_fill_manual(values = c("#d20962","#f47721","#7ac143","#00a78e","#00bce4","red",
                               "#6a2c70","#b83b5e","#f08a5d","#f9ed69","#fc5185","#a8d8ea",
                            "#3fc1c9","#364f6b"))
Fig2
ggsave("2-2FIG.svg",Fig2)

#------#
#sheet3#
#------#
require(tidyr)
df<-read.xlsx("1_data.xlsx", sheet=3,check.names = F, sep = "\t")

df%>%
  dplyr::select(-group)%>%
  gather(variable,value,-sample)%>%
  dplyr::mutate(sample=sample%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(sample)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(sample)))%>%
  dplyr::mutate(x=sample%>%
                  factor(levels=levels(sample),
                         labels=5:1)%>%
                  as.character%>%
                  as.numeric)%>%
  dplyr::left_join(df%>%
                     dplyr::select(-group)%>%
                     gather(variable,value,-sample)%>%
                     dplyr::group_by(sample)%>%
                     dplyr::summarise(sum=sum(value)),
                   by="sample")%>%
  dplyr::mutate(percent_con=value*100/sum)%>%
  dplyr::mutate(variable=variable%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(variable)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(variable),
                         labels=paste("Value",1:ncol(dplyr::select(df,starts_with("Value"))))))->ggData

Fig3<-ggData%>%
  ggplot(aes(x, percent_con))+
  geom_col(aes(fill = variable))+

  coord_polar(theta = 'y')+
  scale_x_continuous(limits = c(-2,9.5))+

  scale_y_continuous(limits = c(0,130))+

  geom_text(data = ggData%>%
              dplyr::select(sample,x)%>%
              unique, aes(x = x, y = 0, label = sample),
            hjust = 1, size = 3.5, show.legend = F)+

  theme_void()+
  theme(legend.position = c(0.2,0.78),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 14, color = "red"))+
  guides(fill=guide_legend(ncol = 1, keywidth=1.3, keyheight=1))+
  scale_fill_manual(values = c("#d20962","#f47721","#7ac143","#00a78e","#00bce4","red"))
Fig3
ggsave("3FIG.svg",Fig3)





#------#
#sheet4#
#------#
require(tidyr)
df<-read.xlsx("1_data.xlsx", sheet=4,check.names = F, sep = "\t")

df%>%
  dplyr::select(-group)%>%
  gather(variable,value,-sample)%>%
  dplyr::mutate(sample=sample%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(sample)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(sample)))%>%
  dplyr::mutate(x=sample%>%
                  factor(levels=levels(sample),
                         labels=5:1)%>%
                  as.character%>%
                  as.numeric)%>%
  dplyr::left_join(df%>%
                     dplyr::select(-group)%>%
                     gather(variable,value,-sample)%>%
                     dplyr::group_by(sample)%>%
                     dplyr::summarise(sum=sum(value)),
                   by="sample")%>%
  dplyr::mutate(percent_con=value*100/sum)%>%
  dplyr::mutate(variable=variable%>%
                  factor(levels=df%>%
                           dplyr::select(-group)%>%
                           gather(variable,value,-sample)%>%
                           dplyr::group_by(variable)%>%
                           dplyr::summarise(sum=sum(value))%>%
                           arrange(desc(sum))%>%
                           use_series(variable),
                         labels=paste("Value",1:ncol(dplyr::select(df,starts_with("Value"))))))->ggData

Fig4<-ggData%>%
  ggplot(aes(x, percent_con))+
  geom_col(aes(fill = variable))+

  coord_polar(theta = 'y')+
  scale_x_continuous(limits = c(-2,9.5))+

  scale_y_continuous(limits = c(0,130))+

  geom_text(data = ggData%>%
              dplyr::select(sample,x)%>%
              unique, aes(x = x, y = 0, label = sample),
            hjust = 1, size = 3.5, show.legend = F)+

  theme_void()+
  theme(legend.position = c(0.2,0.78),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 14, color = "red"))+
  guides(fill=guide_legend(ncol = 1, keywidth=1.3, keyheight=1))+
  scale_fill_manual(values = c("#d20962","#f47721","#7ac143","#00a78e","#00bce4","red",

                               "#6a2c70","#b83b5e","#f08a5d","#f9ed69","#fc5185",
                               "#364f6b"))
#"red","#ff9a00","#3490de","#3ec1d3","#acc236","grey",
Fig4
ggsave("4FIG.svg",Fig4)

